package com.xmwzw.catpu;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment extends Fragment {
    public static final String TAG = BaseFragment.class.getSimpleName();

    private ViewDataBinding mDataBinding;


    public abstract int getContentLayout();

    public abstract void findViews(View view);

    protected View mContentView;


    public <V extends ViewDataBinding> V getBaseViewBinding() {
        return (V) mDataBinding;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public View inflateView(LayoutInflater inflater, ViewGroup container) {
        mDataBinding = DataBindingUtil.inflate(
                inflater,
                getContentLayout(),
                container,
                false);

        return mDataBinding.getRoot();
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mContentView == null) {
            mContentView = inflateView(inflater, container);
            findViews(mContentView);
        } else {
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeView(mContentView);

            }
        }
        return mContentView;
    }

    public void showToast(String message) {
        Activity parent = getActivity();
        if (parent instanceof BaseActivity) {
            BaseActivity activity = (BaseActivity) parent;
            activity.showToast(message);
        }

    }


}
